import {createSlice} from '@reduxjs/toolkit'
import type {Slice} from "@reduxjs/toolkit";
import {formatMenuTree, getFlatMenuList} from "@/router/modules/generateRouter.tsx";

type initialStateType = {
    flatMenuList: Array<any>,
    menuList: Array<any>,
    menuTree: Array<any>,
    collapsed: boolean
}
const initialState: initialStateType = {
    flatMenuList: [],
    menuList: [],
    menuTree: [],
    collapsed: false
}
export const menuSlice: Slice = createSlice({
    name: 'menu',
    initialState,
    reducers: {
        setAuthMenuList: (state: initialStateType, {payload}) => {
            state.menuList = payload
            state.menuTree = formatMenuTree(payload)
            state.flatMenuList = getFlatMenuList(payload)
        },
        setCollapsed: (state: initialStateType, {payload}) => {
            state.collapsed = payload
        },
        clearAuthMenu: (state: initialStateType) => {
            state.flatMenuList = []
            state.menuList = []
            state.menuTree = []
        }
    },
})

export const {setAuthMenuList, clearAuthMenu, setCollapsed} = menuSlice.actions
export default menuSlice.reducer
